package com.crl.rcdp.sys.service;

import cn.hutool.core.bean.BeanUtil;
import com.crl.rcdp.sys.dao.SysResourceDao;
import com.crl.rcdp.sys.dto.SysResourceDTO;
import com.crl.rcdp.sys.dto.SysResourceTreeDTO;
import com.crl.rcdp.sys.model.SysResource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 11:11
 */
@Service
public class SysResourceService {
    @Autowired
    private SysResourceDao sysResourceDao;

    /**
     * 新增
     */
    public int add(SysResourceDTO sysResourceDTO) {
        SysResource sysResource = new SysResource();
        BeanUtil.copyProperties(sysResourceDTO, sysResource);
        if (sysResource.getParentId() == null) {
            sysResource.setParentId(0);
        }
        SysResource sysResourceExist = sysResourceDao.getByResourceCode(sysResourceDTO.getResourceCode());
        if (sysResourceExist != null) {//判断是否代码存在
            return 2;
        }
        return sysResourceDao.add(sysResource);
    }

    /**
     * 根据id修改
     */
    public int update(SysResourceDTO sysResourceDTO) {
        SysResource sysResource = new SysResource();
        BeanUtil.copyProperties(sysResourceDTO, sysResource);
        int flag = sysResourceDao.update(sysResource);
        return flag;
    }


    /**
     * 删除
     */
    public int delete(int id) {
        int childrenNum = sysResourceDao.getChildrenCount(id);
        if (childrenNum > 0) {
            return 2;
        }
        return sysResourceDao.delete(id);
    }

    /**
     * 禁用
     */
    /*public JsonResult updateStatus(SysResourceDTO sysResourceDTO) {
        int flag = sysResourceDao.updateStatus(sysResourceDTO.getId());
        if (flag > 0) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error();
        }
    }*/

    /**
     * 获取资源树
     *
     * @param authSystemId
     */
   /* public JsonResult<List<SysResourceTreeDto>> listSysResources(Integer authSystemId) {
        List<SysResource> sysResourceList = sysResourceDao.listSysResources(authSystemId);
        List<SysResourceTreeDto> tmp = new ArrayList<>();
        for (SysResource sysResource : sysResourceList) {
            SysResourceTreeDto dto = new SysResourceTreeDto();
            BeanUtils.copyProperties(sysResource, dto);
            tmp.add(dto);
        }
        List<SysResourceTreeDto> result = createSysResourceTree(tmp, 0);
        return JsonResultUtil.ok(result);
    }
*/
 /*   private List<SysResourceTreeDto> createSysResourceTree(List<SysResourceTreeDto> list, int parentId) {
        List<SysResourceTreeDto> listChildren = new ArrayList<>();
        for (SysResourceTreeDto tmp : list) {
            if (tmp.getParentId() == parentId) {
                listChildren.add(tmp);
                tmp.setChildren(createSysResourceTree(list, tmp.getId()));
            }
        }
        return listChildren;
    }
*/

    /**
     * 根据资源id获取资源对象
     */
    public SysResourceDTO get(int id) {
        SysResourceDTO sysResourceDTO = new SysResourceDTO();
        SysResource sysResource = sysResourceDao.get(id);
        if (sysResource != null) {
            BeanUtils.copyProperties(sysResource, sysResourceDTO);
        }
        return sysResourceDTO;
    }

    /**
     * 按资源代码获取
     *
     * @param resourceCode
     * @return
     */
    public SysResourceDTO getByResourceCode(String resourceCode) {
        SysResourceDTO sysResourceDTO = new SysResourceDTO();
        SysResource sysResource = sysResourceDao.getByResourceCode(resourceCode);
        if (sysResource != null) {
            BeanUtils.copyProperties(sysResource, sysResourceDTO);
        }
        return sysResourceDTO;
    }

    /**
     * 获取资源树
     */
    public List<SysResourceTreeDTO> getSysResourcesTree() {
        List<SysResource> list = sysResourceDao.listAll();
        List<SysResourceTreeDTO> temp = new ArrayList<>();
        for (SysResource sysResource : list) {
            SysResourceTreeDTO sysResourceTreeDTO = new SysResourceTreeDTO();
            BeanUtils.copyProperties(sysResource, sysResourceTreeDTO);
            temp.add(sysResourceTreeDTO);
        }
        List<SysResourceTreeDTO> result = createResourceTree(temp, 0);
        return result;
    }

    private List<SysResourceTreeDTO> createResourceTree(List<SysResourceTreeDTO> list, int parentId) {
        List<SysResourceTreeDTO> listChildren = new ArrayList<>();
        for (SysResourceTreeDTO tmp : list) {
            if (tmp.getParentId() == parentId) {
                listChildren.add(tmp);
                tmp.setChildren(createResourceTree(list, tmp.getId()));
            }
        }
        return listChildren;
    }
}
